using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;

namespace todoapi.Controllers;

[ApiController]
[Route("aspnet/shop")]
public class ShopController : ControllerBase
{
    private static Goods[] goodies = new Goods[]{new Goods{
        name="Beer", price=0.98, count=10
    },new Goods{
        name="Bread", price=0.32, count=15
    },new Goods{
        name="Cookies", price=0.5, count=8
    },new Goods{
        name="Cupcake",price=1.55, count=12
    },new Goods{
        name="Hamburger", price=2.08, count=20
    }};

    private readonly ILogger<ShopController> _logger;

    public ShopController(ILogger<ShopController> logger)
    {
        _logger = logger;
    }

    [HttpGet(Name = "GetShop")]
    public IEnumerable<Goods> Get()
    {
        return goodies;
    }
    [HttpPost]
    public bool Post([FromBody] Goods goods)
    {
        Goods g = goodies.First(a => goods.name == a.name);
        if (AccountController.account.money >= g.price && g.count > 0)
        {
            AccountController.account.money -= g.price;
            g.count--;
            return true;
        }
        return false;
    }
}
